package com.jrx.anytxn.param.controller.system;


import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.annotation.IgnoreConvertField;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.system.SystemParameterReq;
import com.jrx.anytxn.param.dto.system.SystemParameterRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.system.ISystemParameterService;
import feign.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Classname SystemParameterController
 * @Description 系统参数表
 * @Date 2019/6/14 0014 上午 11:00
 * @author by yf
 */
@Api(tags = "系统参数表参数")
@RestController
@RequestMapping("/param/systems")
public class SystemParameterController {

    private static final Logger logger= LoggerFactory.getLogger(SystemParameterController.class);

    @Autowired
    private ISystemParameterService iSystemParameterService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID分页查询系统参数表信息", notes = "根据租户ID分页查询系统参数表信息")
    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<SystemParameterRes>> getAllSystemParameterByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<SystemParameterRes>> result = new TxnRespResult<>();
        TxnPage<SystemParameterRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = iSystemParameterService.findAllSystemParameter(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据系统参数码ID和租户ID查询此信息", notes = "根据系统参数码ID和租户ID查询此信息")
    @GetMapping(value = "/{paramCode}/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramCode", value = "参数代码", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)
    })
    public TxnRespResult<SystemParameterRes> getByparamCode(@PathVariable String paramCode, @PathVariable("tenantId") String tenantId) {
        TxnRespResult<SystemParameterRes> result = new TxnRespResult<>();
        SystemParameterRes res;
        try {
            res = iSystemParameterService.findByParamCode(paramCode, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据参数码和租户ID编辑系统参数信息", notes = "根据参数码和租户ID编辑系统参数信息")
    @PutMapping("/")
    public TxnRespResult<SystemParameterRes> editSystemParameter(@RequestBody SystemParameterReq systemParameterReq) {
        TxnRespResult<SystemParameterRes> result = new TxnRespResult<>();
        SystemParameterRes response;
        try {
            systemParameterReq.setUpdateBy(infoService.getUser().getUserName());
            response = iSystemParameterService.modifySystemParameter(systemParameterReq);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation("根据租户ID添加系统参数表信息")
    @PostMapping("/")
    public TxnRespResult<SystemParameterRes> createSystemParameter(@RequestBody SystemParameterReq systemParameterReq) {
        TxnRespResult<SystemParameterRes> result = new TxnRespResult<>();
        SystemParameterRes systemParameterRes;
        try {
            systemParameterReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            systemParameterReq.setUpdateBy(infoService.getUser().getUserName());
            systemParameterRes = iSystemParameterService.addSystemParameter(systemParameterReq);
            result.getSuccess(systemParameterRes);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation("根据系统参数码和租户ID删除此信息")
    @DeleteMapping("/{paramCode}/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramCode", value = "参数代码", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)
    })
    public TxnRespResult<Boolean> cancleByParamCode(@PathVariable String paramCode, @PathVariable String tenantId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iSystemParameterService.removeSystemParameter(paramCode, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }


    @ApiOperation(value = "根据系统参数码ID和租户ID查询此信息", notes = "根据系统参数码ID和租户ID查询此信息")
    @PostMapping("/param")
    public TxnRespResult<SystemParameterRes> getParamCode(@RequestParam(value = "paramCode")String paramCode,
                                                          @RequestParam(value = "tenantId")String tenantId){
        TxnRespResult<SystemParameterRes> result = new TxnRespResult<>();
        try {
            SystemParameterRes res = iSystemParameterService.findByParamCode(paramCode, tenantId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }
}
